package com.xca.is.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xca.is.R;
import com.xca.is.adapter.TopNewsAdapter;
import com.xca.is.entity.TopNews;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.TopNewsModel;
import com.xca.is.utils.WebViewScriptUtils;

import java.util.ArrayList;
import java.util.List;

public class IndexFragment extends Fragment implements HttpConnect {

    private TextView wt1, wt2, wt3, wt4, wt5, wt6, wt7, wt8, wt9, wt10;
    private RecyclerView wt_list;
    private TopNewsAdapter adapter;
    private List<TopNews.Result.Data> mList;
    private ProgressBar progressBar;
    private RelativeLayout loadings;
    private TextView[] txts;
    private WebView webView ;

    private TopNewsModel model;
    private ViewModelProvider provider;
    private MutableLiveData<TopNews> liveData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_index, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    private void init(View view) {
        wt1 = view.findViewById(R.id.wt1);
        wt2 = view.findViewById(R.id.wt2);
        wt3 = view.findViewById(R.id.wt3);
        wt4 = view.findViewById(R.id.wt4);
        wt5 = view.findViewById(R.id.wt5);
        wt6 = view.findViewById(R.id.wt6);
        wt7 = view.findViewById(R.id.wt7);
        wt8 = view.findViewById(R.id.wt8);
        wt9 = view.findViewById(R.id.wt9);
        wt10 = view.findViewById(R.id.wt10);
        txts = new TextView[]{wt1, wt2, wt3, wt4, wt5, wt6, wt7, wt8, wt9, wt10};
        wt_list = view.findViewById(R.id.w_list);
        progressBar = view.findViewById(R.id.progressBar);
        loadings = view.findViewById(R.id.r_loading);
        webView = view.findViewById(R.id.index_web);
        loadings.setVisibility(View.GONE);
        wt1.setSelected(true);

        int[] ints = new int[]{10,18};
        WebViewScriptUtils.htmlContent(ints,webView);

        wt_list.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        mList = new ArrayList<>();

        provider = new ViewModelProvider(this);
        model = provider.get(TopNewsModel.class);
        model.setModel(this);

       // model.getData("toutiao");


        liveData = (MutableLiveData<TopNews>) model.getLiveData();
        liveData.observe(getActivity(), new Observer<TopNews>() {
            @Override
            public void onChanged(TopNews topNews) {
                mList = topNews.getResult().getData();
                //Toast.makeText(getActivity(), String.valueOf(mList.size()), Toast.LENGTH_SHORT).show();
                adapter.updateDate(mList);
            }
        });

        for (int i = 0; i < txts.length; i++) {
            titleClick(i);
        }

        adapter = new TopNewsAdapter(mList, getContext(), getActivity());
        wt_list.setAdapter(adapter);

    }

    private void titleClick(int index) {
        txts[index].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textSelector(index);
             //  getData(index);
            }
        });
    }

    private void textSelector(int index) {
        for (int i = 0; i < txts.length; i++) {
            txts[i].setSelected(i == index);
        }
    }

    private void getData(int index) {
        switch (index) {
            case 0:
                model.getData("toutiao");
                break;
            case 1:
                model.getData("shehui");
                break;
            case 2:
                model.getData("guonei");
                break;
            case 3:
                model.getData("guoji");
                break;
            case 4:
                model.getData("yule");
                break;
            case 5:
                model.getData("tiyu");
                break;
            case 6:
                model.getData("junshi");
                break;
            case 7:
                model.getData("keji");
                break;
            case 8:
                model.getData("caijing");
                break;
            case 9:
                model.getData("shishang");
                break;
        }
    }

    @Override
    public void Loading(boolean loading) {
        // loadings.setVisibility(loading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {

    }

    @Override
    public void Complete() {
        loadings.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}